var app = getApp();
Page( Object.assign({},{
    data:{
        info:'',
        allwork : [],
        color : '',
        workinfo : {},
        isend : false,
        score : 0,
        all : 0,
        rightnum : 0,
        iswait : true,
        temp : {},
        rightarr : [],
        showscore : false,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },

    onLoad:function(options){
        var self = this;
        var id = options.id ? options.id : '';

        self.setData({
            options : options,
        })

        app.com.http('study','POST',{op:'workinfo',id:id},0,false,'','',function(res){
            if (!res.data.errno && res.data) {

                var color = app.com.mainColor(res.data.data.params.style);

                self.setData({
                    info : res.data.data.info,
                    allwork : res.data.data.allwork,
                    color : color,
                    params : res.data.data.params,
                    workinfo : res.data.data.workinfo,
                    isend : false,
                    iswait : true,
                    all : 0,
                    score:0,
                    rightarr : [],
                    rightnum : 0,
                },function(){
                    self.setData({
                        inited : true,
                    });
                });

                wx.setNavigationBarTitle({
                    title: res.data.data.info.name
                });

            }else{
                app.com.alert(res.data.message,function(){
                    app.com.backPage(0);
                });
            }
        });
    },
    onPullDownRefresh: function () {
        if( this.data.isdown || this.data.isend ) return false;
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
        wx.stopPullDownRefresh();
    },
    reset(){
        this.setData({
            iswait : true,
            isend : false,
        })
        this.onPullDownRefresh();
    },
    submit(e){
        var self = this;
        var current = self.data.workinfo.current*1;
        var postdata = {
            id : self.data.workinfo.id,
            logid : self.data.workinfo.logid,
            next : current >= self.data.allwork.length ? -1 : self.data.allwork[current].id,
            op : 'answer',
        };

        if(self.data.ising) return false;
        self.data.ising = true;
        var key = [];
        for (var i = 0; i < self.data.workinfo.quest.length; i++) {
            var item = self.data.workinfo.quest[i];
            if(item.ischecked == 1){
                key.push(i+1);
            }
        }
        if(key.length <= 0){
            app.com.alert('还没选择答案');
            self.data.ising = false;
            return false;
        }
        postdata.key = key;

        app.com.http('study','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                
                if( res.data.data.iscom == 1 ){
                    
                    if( res.data.data.isright == 1 ){
                        var tip = '回答正确';
                        var icon = 'success';
                        var keyy = 'workinfo.right';
                        var num = self.data.workinfo.right*1 + 1;
                        var errnum = self.data.workinfo.errnum*1;
                    }else{
                        var tip = '回答错误';
                        var icon = 'none';
                        var keyy = 'workinfo.error';
                        var num = self.data.workinfo.error*1 + 1;
                        var errnum = res.data.data.readstatus == 0 ? self.data.workinfo.errnum*1 + 1 : self.data.workinfo.errnum*1;
                    }
                    self.setData({
                        [keyy] : num,
                        'workinfo.errnum' : errnum,
                        iswait : false,
                        rightarr : res.data.data.rightarr,
                        temp : res.data.data.workinfo,
                    })
                    wx.showToast({
                        title: tip,
                        icon: icon,
                        mask:true,
                        duration: 1500
                    })
                    if(res.data.data.isend == 1){
                        self.setData({
                            isend : true,
                            showscore : res.data.data.isright == 1 ? true : false,
                            score : res.data.data.score,
                            all : res.data.data.all,
                            rightnum : res.data.data.rightnum,
                        })
                    }else if( res.data.data.isright == 1 ){
                        self.next();
                    }
                }
            }else{
                app.com.alert(res.data.message);
            }
            self.data.ising = false;
        });
    },
    next(){
        var self = this;
        self.setData({
            iswait : true,
            rightarr : [],
            'workinfo.id' : self.data.temp.id,
            'workinfo.ismulti' : self.data.temp.ismulti,
            'workinfo.name' : self.data.temp.name,
            'workinfo.quest' : self.data.temp.quest,
            'workinfo.img' : self.data.temp.img,
            'workinfo.current' : self.data.workinfo.current*1 + 1,
        })
    },
    select(e){
        if(!this.data.iswait) return false;
        var index = e.currentTarget.dataset.index;
        if( this.data.workinfo.ismulti == 0 ){
            for (var i = 0; i < this.data.workinfo.quest.length; i++) {
                if( this.data.workinfo.quest[i].ischecked ){
                    this.setData({
                        ['workinfo.quest['+i+'].ischecked'] : false
                    })
                }
            }
        }
        this.setData({
            ['workinfo.quest['+index+'].ischecked'] : !this.data.workinfo.quest[index].ischecked
        })
    },
    getscore(){
        this.setData({
            showscore : true
        })
    },
    onClose(){
        this.setData({
            showscore : false
        })
    },
    back(){
        wx.navigateBack();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e);
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    
}))